home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / grabtext.arc / BSAVEINC.C < prev    next >
Text File  |  1990-08-28  |  7KB  |  251 lines

  1. /*
  2.    BSAVEINC.c by Bill Buckels 1990
  3.    written in AZTEC C small model
  4.  
  5.    Supported Screen Mode is CGA COLOR TEXT Mode 4.
  6.  
  7.   This is a companion utility to be used with BSAVED text images
  8.   I hope this code proves informative to those of you interested in
  9.   computer graphics.
  10.  
  11.   This is also a limited example of applied "Run Length Encoding."
  12.  
  13.   The IBM-PC 80- column text screen buffer-attribute pair arrangement
  14.   is suited to a 2-pass crunching algorithm.
  15.  
  16.   In purely poetic terms....
  17.  
  18.   Blocks of repeats occur on increments of every second byte so
  19.   I leapfrog through the file using the .PCX encoding technique
  20.   but I skip every second byte, and encode the ascuii value of
  21.   the text character only, then when I am done I make a second
  22.   pass to encode the bytes that have been skipped on first pass,
  23.   encoding the screen attributes.
  24.  
  25.   Function:
  26.  
  27.   converts a BASIC BSAVED .BSV IMAGE FILE (full screen CGA text)
  28.   to a .inc inlude file for a C program in the form of character array.
  29.   ( similar to the ZSOFT .PCX format image file per the
  30.   VERSION 2.8 STANDARD without Color Map.)
  31.  
  32.  
  33. */
  34.  
  35. #include <stdio.h>
  36. #include <fcntl.h>
  37. #include <stdlib.h>
  38. #include <io.h>
  39.  
  40. #define SCREENSIZE 4000
  41.  
  42. FILE *incfile;
  43.  
  44. /* a microsoft compatible bsaved memory image format descriptor */
  45. unsigned char BSV_header[7]={
  46.  
  47.     '\xfd',          /* ID Flag = file descriptor identifier bsaved file */
  48.  
  49.    /* afew words about what BASIC does with this header */
  50.    /* and afew words about extending BASIC's ability to */
  51.    /* read and use BINARY files created in this manner. */
  52.  
  53.     /* BASIC will use original segment and offset information  */
  54.     /* to reload a memory image unless "DEF SEG" has been used */
  55.     /* and then an explicit offset is used as the second arg   */
  56.     /* of the bload command. */
  57.  
  58.     /* subsequently, once DEF SEG is invoked  */
  59.     /* If an offset is specified without then */
  60.     /* first calling DEF SEG, The image will then be loaded to */
  61.     /* the segment specified in the last segment pointed to    */
  62.     /* by the last call to DEF SEG. DEF SEG without args returns */
  63.     /* to DGROUP (the default data segment) and resets the thing.*/
  64.  
  65.     /* we can also implement and store an array in memory by use of */
  66.     /* VARSEG and VARPTR to obtain the window for the array's       */
  67.     /* memory location, i.e. override the defaults by windowing     */
  68.     /* to the array base using DEF SEG = VARSEG(arrayname) then to     */
  69.     /* fill the array we would use BLOAD bsaved.bsv, VARPTR(arrayname) */
  70.     /* using VARPTR to point to the offset from the memory base segment*/
  71.  
  72.     '\x00', '\xb8',  /* base address     = MSB | LSB original segment    */
  73.     '\x00', '\x00',  /* offset from base = MSB | LSB original offset     */
  74.  
  75.     '\xA0', '\x0F'   /* file size = MSB | LSB of bytes to be loaded +    */
  76.                      /* size of descriptors in bytes (8)                 */
  77.     };
  78.  
  79. unsigned char BSAVED_tailer[1]={   /* for reference only */
  80.     '\x1A'
  81.     };
  82.  
  83.  
  84. int encline(inbuff,inlen)/* encodes a raw line and writes it out */
  85. unsigned char *inbuff;
  86. int inlen;
  87. {
  88.     unsigned char this,last;
  89.     int srcindex,i;
  90.     register int total;
  91.     register unsigned char runcount;
  92.     total=0;
  93.     last = *(inbuff); runcount=1;
  94.  
  95. for(srcindex=1;srcindex!=inlen;srcindex++){
  96.     this= *(++inbuff);
  97.     if(this==last){
  98.         runcount++;
  99.         if(runcount==63){
  100.             if(!(i=encput(last,runcount)))
  101.             return(0);
  102.             total+=i;
  103.             runcount=0;
  104.         }
  105.     }
  106.     else{
  107.         if(runcount){
  108.             if(!(i=encput(last,runcount)))
  109.             return(0);
  110.             total+=i;
  111.         }
  112.         last=this;
  113.         runcount=1;
  114.       }
  115.    }
  116.  
  117. if(runcount){
  118.     if(!(i=encput(last,runcount)))
  119.     return(0);
  120.     return(total+i);
  121.     }
  122.     return (total);
  123.  
  124. }
  125.  
  126.  
  127. int linelen=0;
  128. int encput(byt,cnt)       /* the writer for the encline function */
  129. unsigned char byt,cnt;
  130. {
  131.           if(linelen>16){
  132.                       fprintf(incfile,"\n");
  133.                       linelen=0;
  134.                       }
  135.  
  136.           if(cnt){
  137.             if((cnt==1)&& (0xc0 != (0xc0 &byt))){
  138.                 fprintf(incfile,"%3d,",(int)byt);
  139.                 linelen++;
  140.                 return(1);
  141.                  }
  142.             else{
  143.                 fprintf(incfile,"%3d,",(int)(0xc0|cnt));
  144.                 fprintf(incfile,"%3d,",(int)byt);
  145.                 linelen+=2;
  146.                 return(2);
  147.                 }
  148.                 }
  149.         return(0);
  150. }
  151.  
  152.  
  153.  
  154.  
  155. int BSV2INC(char *name1, char *name2, char *name3)
  156. {
  157.     unsigned char *scratchbuffer;
  158.     unsigned char *buf;
  159.  
  160.     unsigned char header[7];
  161.     int i,j,k;
  162.  
  163.     int fh;
  164.  
  165.     if((fh = open(name1,O_RDONLY)) == -1)return -1;
  166.     read(fh,header,7);
  167.  
  168.     /* make sure that we have a valid BSAVED file format */
  169.     /* we can only check the first 2-bytes since it may  */
  170.     /* have been made on the HERCULES */
  171.  
  172.     if(header[0]!=BSV_header[0] || header[1]!=BSV_header[1])
  173.         {
  174.          close(fh);
  175.          return -2;
  176.          }
  177.  
  178.     printf("BSAVEINC(C) Copyright by Bill Buckels\n\n");
  179.     printf("Input  File: %s\n",name1);
  180.     printf("Output File: %s\n",name2);
  181.  
  182.     scratchbuffer= malloc(SCREENSIZE);
  183.     buf=malloc(SCREENSIZE/2);
  184.  
  185.     read(fh,scratchbuffer,SCREENSIZE);
  186.     close(fh);
  187.  
  188.     /* seperate the attribute bytes */
  189.     /* use the two-tank system.     */
  190.  
  191.     incfile=fopen(name2,"w");
  192.     fprintf(incfile,"unsigned char %s[]={\n",name3);
  193.  
  194.                 for(k=0;k!=2;k++)
  195.                    {
  196.                     j=0;
  197.                     for(i=k;i<SCREENSIZE;i+=2)
  198.                     {
  199.                      buf[j]=scratchbuffer[i];
  200.                      j++;
  201.                      }
  202.                      encline(buf,SCREENSIZE/2);
  203.                      }
  204.  
  205.             fprintf(incfile,"\n0}; /* terminator */\n");
  206.             fclose(incfile);
  207.             free(scratchbuffer);
  208.             free(buf);
  209.             printf("Done!\n");
  210.             return 0;
  211. }
  212.  
  213. main(int argc,char *argv[])
  214. {
  215.    char buffer[66];
  216.    char  name1[66];
  217.    char  name2[66];
  218.    char  name3[66];
  219.  
  220.    int status=0;
  221.  
  222.           switch(argc)
  223.           {
  224.             case 2: strcpy(buffer,argv[1]);
  225.  
  226.                     while(buffer[status]!=0)
  227.                           {
  228.                             if(buffer[status]=='.')buffer[status]=0;
  229.                             else status++;
  230.                             }
  231.  
  232.                     strcpy(name1,buffer);
  233.                     strcat(name1,".BSV");
  234.                     strcpy(name2,buffer);
  235.                     strcat(name2,".INC");
  236.                     strcpy(name3,buffer);
  237.  
  238.                         status=BSV2INC(name1,name2,name3);
  239.                         if(status==0)exit(0);
  240.  
  241.  default: printf("BSAVEINC(C) Copyright by Bill Buckels 1990\n");
  242.           printf(
  243.  "Usage : \"BSAVEINC [filename]\"\n");
  244.           printf(
  245.           "Converts .BSV BSAVED IMAGES to .INC encoded C arrays.\n");
  246.           printf("CGA COLOR TEXT mode, screen dumps only.\n");
  247.           }
  248.           exit(0);
  249. }
  250.  
  251.